﻿using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using Newtonsoft.Json;

namespace ChinaMobile
{
    public class MingTongProcessInfo
    {
        public string taskSN { get; set; }


        public OrderType OrderType { get; set; }
        public IDictionary<string, string> fieldMap { get; set; } = new Dictionary<string, string>();
    }

    public class MingTongProcessResult
    {
        //"code": "0",//-1：失败；0：成功
        //"taskSn": "WF063063000000001",
        //"desc": "接收成功",
        //"taskUrl": "www.xxxx.com/xsfsfs"

        public string Code { get; set; }

        public string TaskSn { get; set; }
        public string Desc { get; set; }

        public string TaskUrl { get; set; }
    }

    public enum OrderType
    {
        PreOrder = 1,
        FormalOrder = 7
    }

    public class MingTongWorkOrderManager
    {
        private readonly string _url;


        public MingTongWorkOrderManager(string url = "http://10.244.78.69:8080/mtnoh/AppInterface/gdwd/ProcessTask.aspx")
        {
            _url = url;
        }

        public MingTongProcessResult PostTo(MingTongProcessInfo info)
        {
            var client = new HttpClient();
            var postObject = new
            {
                userName = "admin",
                versionId = 51,
                submit = 1,
                curStatus = Convert.ToInt32(info.OrderType),
                info.taskSN,
                info.fieldMap
            };
            var httpClient = new HttpClient();
            var formData = new Dictionary<string, string>()
            {
                { "data",JsonConvert.SerializeObject(postObject)}
            };
            var formContent = new FormUrlEncodedContent(formData);
          
            var resp = httpClient.PostAsync(_url, formContent);

            var content = resp.Result.Content.ReadAsStringAsync().Result;
            var f = JsonConvert.DeserializeObject<MingTongProcessResult>(content);
            return f;

        }
    }
}